//
//  HLIMKitMessageTipContentView.m
//  YueYue
//
//  Created by hulu001 on 2024/1/12.
//

#import "HLIMKitMessageTipContentView.h"
#import "M80AttributedLabel.h"
#import "HLChatKit.h"

@interface HLIMKitMessageTipContentView ()  <M80AttributedLabelDelegate>

@end

@implementation HLIMKitMessageTipContentView

- (instancetype)init
{
    self = [super init];
    
    if (self) {
        UIView *msgBgView = [UIView.alloc initWithFrame:CGRectZero];
        msgBgView.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
        msgBgView.layer.cornerRadius = 10;
        msgBgView.layer.masksToBounds = YES;
        [self addSubview:msgBgView];
        self.msgBgView = msgBgView;
        
        M80AttributedLabel *msgLabel = [M80AttributedLabel.alloc init];
        msgLabel.delegate = self;
        msgLabel.backgroundColor = [UIColor colorWithRed:240/255.0 green:240/255.0 blue:240/255.0 alpha:1.0];
        msgLabel.textAlignment = kCTTextAlignmentCenter;
        [msgBgView addSubview:msgLabel];
        self.msgLabel = msgLabel;
        
        UILabel *timelabel = [[UILabel alloc] initWithFrame:CGRectZero];
        timelabel.font = [UIFont systemFontOfSize:14];
        timelabel.textColor = [UIColor colorWithRed:168/255.0 green:168/255.0 blue:168/255.0 alpha:1.0];
        [self addSubview:timelabel];
        self.timeLabel = timelabel;
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    
    CGFloat tableViewWidth = self.superview.width;
    CGSize contentsize  =   [self.messageModel contentSize:tableViewWidth];
    
    [_timeLabel sizeToFit];
    _timeLabel.centerX = self.middleX;
    _timeLabel.top = 2;
    
    CGSize msgSize = [self.msgLabel sizeThatFits:CGSizeMake(tableViewWidth * 0.8, MAXFLOAT)];
    
    self.msgBgView.viewSize = CGSizeMake(msgSize.width+32, contentsize.height - 23);
    self.msgBgView.centerX = self.middleX;
    self.msgBgView.top = _timeLabel.bottom+4;
    
    self.msgLabel.viewSize = CGSizeMake(msgSize.width, msgSize.height+1);
    self.msgLabel.center = self.msgBgView.middlePoint;
}

- (void)refreshData:(HLIMKitMessageModel *)messageModel
{
    [super refreshData:messageModel];
    
    HLIMKitMessageSetting *setting = [HLChatKit.shareSDK.config setting:messageModel.message];
    
    NIMMessage * message = messageModel.message;
    self.msgLabel.font = setting.font;
    self.msgLabel.textColor = setting.textColor;
    
    NSDictionary *ext = message.localExt;
    if ([ext[@"TipType"] isEqualToString:@"1"]) {
        self.msgLabel.text = @"你撤回了一条消息";
//        self.msgLabel.underLineForLink = NO;
//        [self.msgLabel addCustomLink:ext[@"OriginMessage"] forRange:NSMakeRange(9, 4) linkColor:[UIColor colorWithHexString:@"#FB0F52"]];
    } else {
        self.msgLabel.text = message.text;
    }
    
    self.timeLabel.text = [HLHelper showTime:message.timestamp showDetail:YES];
}

- (void)m80AttributedLabel:(M80AttributedLabel *)label clickedOnLink:(id)linkData
{
    HLIMKitEvent *event = [HLIMKitEvent.alloc init];
    event.gestureName = HLIMKitGestureTouchUpInside;
    event.messageModel = self.messageModel;
    event.data = linkData;
    [self.delegate onTap:event];
}


@end
